package org.urbansoft.icl.util;

import org.apache.log4j.Logger;

/**
 * The Class ModTen.
 */
public class ModTen {
	private static Logger log = Logger.getLogger(ModTen.class);

	/**
	 * Instantiates a new mod ten.
	 */
	private ModTen() {
		super();
	}

	/**
	 * Check aba.
	 * 
	 * @param aba
	 *            the aba
	 * 
	 * @return true, if successful
	 */
	public static boolean checkAba(String aba) {
		String abaEight = aba.substring(0, 8);
		String checkDigit = aba.substring(aba.length() - 1);
		String calcDigit;
		String weights = "37137137";
		int sum = 0;
		String modulo;

		if (abaEight.length() != 8) {
			return false;
		}

		for (int i = 0; i < 8; i++) {
			int partOne = Integer.parseInt(abaEight.substring(i, i + 1));
			int partTwo = Integer.parseInt(weights.substring(i, i + 1));
			int multiple = partOne * partTwo;
			sum = sum + multiple;
		}

		modulo = Integer.toString(10 - (sum % 10));
		calcDigit = modulo.substring(modulo.length() - 1);

		if (calcDigit.equals(checkDigit)) {
			return true;
		}
		return false;

	}

	public static String getAba(String aba) {
		String abaEight = aba;
		String calcDigit;
		String weights = "37137137";
		int sum = 0;
		String modulo;

		if (abaEight.length() != 8) {
			return null;
		}

		for (int i = 0; i < 8; i++) {
			int partOne = Integer.parseInt(abaEight.substring(i, i + 1));
			int partTwo = Integer.parseInt(weights.substring(i, i + 1));
			int multiple = partOne * partTwo;
			sum = sum + multiple;
		}

		modulo = Integer.toString(10 - (sum % 10));
		calcDigit = modulo.substring(modulo.length() - 1);

		return calcDigit;

	}
}
